using AOP;
using BusinessRules;
using BusinessRules.DynamicProperties;
using Formula;

namespace Sample
{
    public class DynamicEntity<T> : EntityBase<T> where T : EntityBase<T>
    {
        private DynamicPropertyCollection _properties;
        public DynamicPropertyCollection DynamicProperties
        {
            get
            {
                if (_properties == null)
                {
                    _properties = new DynamicEntityMetadata()
                        .AddProperty<int>("DynamicValue")
                        .AddProperty<string>("String")
                        .CreateInstance();
                }
                return _properties;
            }
        }

        public DynamicEntity()
        {
            Rules.Compiler = FormulaCompiler.Instance
                .WithDynamicEntity<T>(DynamicProperties.Metadata);
        }
    }

    [NotifyPropertyChanged]
    internal class InvoiceLine : DynamicEntity<InvoiceLine>
    {
        public int Value { get; set; }
        public int Value1 { get; set; }
        public int Value2 { get; set; }

        public InvoiceLine()
        {
            Rules.AddBusinessRule(e => e.Value + e.Value1, e => Value2);
            Rules.AddBusinessRule("DynamicValue=Value2");
            Rules.AddBusinessRule("String=Value2.ToString");
            Rules.Enable();
        }
    }
}